<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>新增数据源</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}" media="screen"/>
    <link rel="stylesheet" th:href="@{/static/css/layui.css}" media="all"/>
</head>
<body>
<div class="layui-field-box">
    <form class="layui-form" action="" id="addFrom">
        <div class="layui-inline">
            <div class="layui-form-item">
                <label class="layui-form-label">数据库类型</label>
                <div class="layui-input-inline">
                    <select name="dbType" id="dbType" lay-filter="dbType" lay-search=""
                            lay-affix="clear"></select>
                </div>
                <div class="layui-input-inline">
                    <button class="layui-btn layui-btn-primary layui-border-orange" lay-submit lay-filter="testUrl">
                        <i class="layui-icon layui-icon-test"></i>测试
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">连接名称</label>
            <div class="layui-input-block">
                <input type="text" name="dbName" id="dbName" lay-verify="required" autocomplete="off" lay-affix="clear"
                       placeholder="请输入数据源名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">驱动名称</label>
            <div class="layui-input-block">
                <input type="text" name="driverClass" id="driverClass" lay-verify="required" lay-affix="clear"
                       placeholder="请输入驱动名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">连接地址</label>
            <div class="layui-input-block">
                <input type="text" name="dbUrl" id="dbUrl" lay-verify="required" placeholder="请输入连接地址"
                       lay-affix="clear"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">验证语句</label>
                <div class="layui-input-inline">
                    <input type="text" name="dbCheckUrl" id="dbCheckUrl" placeholder="请输入验证查询语句"
                           lay-affix="clear"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">登录账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="dbAccount" id="dbAccount" placeholder="请输入数据库登录账号"
                           lay-affix="clear"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">登录密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="dbPassword" id="dbPassword" placeholder="请输入数据库登录密码"
                           lay-affix="eye"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">初始大小</label>
                <div class="layui-input-inline">
                    <input type="text" name="initialSize" id="initialSize" value="5" placeholder="请输入初始连接数"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">最大连接数</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxActive" id="maxActive" value="20" placeholder="请输入最大连接数"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">最大空闲数</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxIdle" id="maxIdle" value="5" placeholder="请输入最大空闲数"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">最大等待时间(ms)</label>
                <div class="layui-input-inline">
                    <input type="text" name="maxWait" id="maxWait" value="2000" placeholder="请输入最大等待时间"
                           lay-affix="number"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="dbState" lay-verify="required">
                        <option value="有效">有效</option>
                        <option value="无效">无效</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="page-footer">
            <div class="btn-list">
                <div class="btnlist">
                    <button class="layui-btn layui-btn-primary layui-border-green" lay-submit lay-filter="save"
                            lay-type="saveContinue">
                        <i class="layui-icon  layui-icon-add-circle">&nbsp;</i>保存并继续
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-blue" lay-submit lay-filter="save"
                            lay-type="saveClose">
                        <i class="layui-icon layui-icon-add-1">&nbsp;</i>保存并关闭
                    </button>
                    <button class="layui-btn layui-btn-primary layui-border-red" onclick="refresh();">
                        <i class="layui-icon layui-icon-close-fill">&nbsp;</i>关闭
                    </button>
                </div>
            </div>
        </div>
    </form>

</div>
<script th:src="@{/static/layui.js}"></script>
<script th:src="@{/static/lay-config.js}"></script>
<script th:replace="~{base::ctx}"></script>
<script>

    layui.use(['jquery', 'layer', 'form', 'laySelect'], function () {
        const layer = layui.layer;
        const form = layui.form;
        const $ = layui.jquery;
        const select = layui.laySelect;

        /**
         * 加载模板驱动数据
         */
        select.render({
            elem: "#dbType",
            url: ctx + 'dataSourceManager/findDriverConfigListSelect',
            select: 10000,
            title: '请选择数据库类型',
            format: function (data) {
                return parseSelectData(data);
            },
            success: function (data, activeData) {
            },
            onselect: function (data) {
                //查询详细配置项
                if (data === "" || data === null) {
                    return false;
                }
                post(ctx + "dataSourceManager/findDriverConfigListSelect?id=" + data, null, (res) => {
                    settingInputs(res.data[0]);
                })
            }
        });

        /**
         * 保存
         */
        form.on('submit(save)', function (data) {
            const field = data.field;
            const elem = data.elem;
            const layType = elem.getAttribute("lay-type");
            post(ctx + "dataSourceManager/addDataSource", field, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2}, () => {
                    if (res.code === 200) {
                        if (layType === "saveClose") {
                            refresh();
                        } else {
                            location.reload();
                        }
                    }
                });
            })
            return false;
        });

        /**
         * 测试链接
         */
        form.on('submit(testUrl)', function (data) {
            const field = data.field;
            post(ctx + "dataSourceManager/checkUrl", field, (res) => {
                layer.msg(res.msg, {icon: res.code === 200 ? 1 : 2});
            })
            return false;
        });


        //处理表单赋值
        function settingInputs(data) {
            $("#dbName").val("websql-" + data.name);
            $("#driverClass").val(data.driverClass);
            $("#dbUrl").val(data.url);
            $("#dbCheckUrl").val(data.dbCheckUrl);
        }

    });
</script>
</body>
</html>
